package vn.easyforshopping.res;

import java.util.HashMap;

import vn.easyforshopping.R;
import vn.easyforshopping.network.GlobalServices;
import vn.easyforshopping.utils.IActionListener;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

/**
 * Class nay dung de quan li toan bo resource.
 * @author truongps
 *
 */
public class ResourceManager {
	
	public static Resources res;
	/**
	 * Chua cac duog link resource dang duoc download, hoac da duoc cache vao..
	 */
	static HashMap<String, Bitmap> bitmapCache = new HashMap<String, Bitmap>();
	private final String SDCARD_PATH; 
	
	SDCardReader sdCardReader;
	SDCardWriter sdCardWriter;
	
	
	static ResourceManager instance;
	public static ResourceManager getInstance() {
		if (instance == null) {
			instance = new ResourceManager();
		}
		return instance;
	}
	
	private ResourceManager() {
		String root = Environment.getExternalStorageDirectory().toString();
		SDCARD_PATH = root + "/EasyForShooping";
		
		sdCardReader = new SDCardReader(SDCARD_PATH);
		sdCardWriter = new SDCardWriter(SDCARD_PATH);
	}
	
	
	/**
	 * 
	 * @param onCompleted
	 * @return true - Da lay duoc res dug cua no; false - Da lay duoc hinh default
	 */
	public boolean getBitMap(String bitmapLink, IActionListener onCompleted) {
		if (onCompleted == null) {
			throw new NullPointerException("onCompleted cannot be null");
		}
		Bitmap b = bitmapCache.get(bitmapLink);
		if (b == null) {
			if (sdCardReader.isFileAvailable(bitmapLink)) {
				//Doc bit map len.
				
			} else {
				//Lay bitmap tu web
//				GlobalServices.getBitmap(bitmapLink, );
			}
		}
		
		return false;
	}
	
	public void addBitMapIntoCache(String link, Bitmap bitmap) {
		synchronized (bitmapCache) {
			bitmapCache.put(link, bitmap);
		}
		/**
		 * Luu xuong the nho sd.
		 */
		sdCardWriter.writeBitmap(link, bitmap);
	}
	
	
	
	
	/**
	 * Luc dau se gan cho no default bitmap truoc.
	 * @return
	 */
	public Bitmap getDedaultBitmap() {
		return BitmapFactory.decodeResource(res, R.drawable.ic_launcher);
	}
}
